package cn.quevo.ucenter.client.dao.impl;

import cn.quevo.core.util.PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.quevo.beetlsql.BasicDao;
import cn.quevo.core.exception.CheckParamsException;
import cn.quevo.core.lang.StringUtils;
import cn.quevo.ucenter.client.autoconfigure.UcenterProperties;
import cn.quevo.ucenter.client.dao.ClientRestDao;
import cn.quevo.ucenter.client.entity.query.UserQuery;
import cn.quevo.ucenter.core.entity.User;
import cn.quevo.ucenter.core.enums.OauthResultStatusEnum;
import cn.quevo.ucenter.core.exception.OauthException;

import java.security.NoSuchAlgorithmException;

@Repository("clientRestDao")
public class ClientRestDaoImpl extends BasicDao<User, UserQuery> implements ClientRestDao{

    @Autowired
    private UcenterProperties ucenterProperties;

    /**
     * 根据Token获取用户信息
     *
     * @author July july_sky@foxmail.com
     * @date 2018-04-02 10:32:12
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     * @param token
     * @return
     */
    @Override
    public User getUserByToken(String token) throws OauthException, CheckParamsException {
        if(ucenterProperties == null || StringUtils.isBlank(ucenterProperties.getTokenUserSql())) {
            throw new OauthException(OauthResultStatusEnum.INVALID_TOKEN);
        }
        User user = super.getBeetlSqlFactory().executeObject(ucenterProperties.getTokenUserSql(), User.class, token);
        if(ucenterProperties.isTest()) {
           if (StringUtils.isBlank(user.getRoleStr())) {
               user.setRoleStr("TEST");
               return user;
           }
           if (!StringUtils.contains("TEST",user.getRoleStr().split(","))) {
               user.getRoles().contains("TEST");
               return user;
           }
        }
        return user;
    }
    
    @Override
    public String getSqlPre() {
        return null;
    }
}
